if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    var houtai = {
        linshi_search: {
            search: "",
            supplier:"",
            types:"",
            warnAmount:null
        },
        linshi_record:{
            search:[],
            type:"",
            pay_type:"",
            start_time:"",
            end_time:"",
            supplier:""
        },
        linshi_id: null, //临时的id存储 用来判断同一个按钮的操作id
        record_id:null,
        linshi_record_page:1,
        linshi_arrid: [],
        repertory_type: null, // 0入库 1出库 提交时根据类型判断请求
        type:'',              //1 商品删除  2记录删除
        list_ajax_page:1,
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        //选中select
        select_in_text:function(text,domEle){
            var value='999';
            $.each(domEle.find('option'),function () {
                if($(this).text()===text){
                    value=$(this).attr('value');
                    return false;
                }
            });
            return value;
        },
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        // 检查功能一般检查是否为空
        jiancha: function () {
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint($(".add_goods_body .input_is:visible").eq(i).prev().text() + " 没有填写！");
                    return false;
                }
            }
            return true;
        },
        // 导出excel
        excel:(function () {
            var uri = 'data:application/vnd.ms-excel;base64,',
                template = '<html  xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta charset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
                base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))); },
                format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); };
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById("tableExcel");
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };
                if($("#dlink").length<1)$("body").append($("<a id='dlink' style='display: none'></a>"));

                document.getElementById("dlink").href = uri + base64(format(template, ctx));
                document.getElementById("dlink").download = name+'.xls';
                document.getElementById("dlink").click();
            };
        })(),
        // 获取商品的数组
        shop_list_ajax: function (page_number) {
            var n = page_number===undefined ?houtai.list_ajax_page=1  : page_number;
            var ispring=n!==0;
            console.log(n)
            $.ajax({
                url: ip + 'index.php/admin/stuffStorage/show',
                type: "post",
                async:ispring,
                data: {
                    page: n,
                    search:houtai.linshi_search.search,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    houtai.table_string(res);
                    $("#page").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.list_ajax_page=pageIndex;
                            houtai.shop_list_ajax(pageIndex);
                        }
                    });
                }

            });
        },

        add:function(){
            $.ajax({
                url: ip + 'index.php/admin/stuff/add',
                type: "post",
                data: {
                    name:$.trim($('#add_item_name').val()),
                    spec:$.trim($('#add_item_spec').val()),
                    unit:$.trim($('#add_item_unit').val()),
                    types:$('#add_item_class').val(),
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res)
                    console.log(res)
                    if(res.resStatus===0){
                        $('#add_item_wrap').fadeOut()
                        houtai.hint('<p class="text-success">添加成功了</p>')
                        houtai.shop_list_ajax()
                        $('#add_item_wrap .input_is').val('')
                    }else{
                        houtai.hint(res.message)
                    }
                }

            });
        },
        set:function(){
            $.ajax({
                url: ip + 'index.php/admin/stuff/edit',
                type: "post",
                data: {
                    stuffId:houtai.linshi_id,
                    name:$.trim($('#set_item_name').val()),
                    spec:$.trim($('#set_item_spec').val()),
                    unit:$.trim($('#set_item_unit').val()),
                    types:$('#set_item_class').val(),
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res)
                    console.log(res)
                    if(res.resStatus===0){
                        $('#set_item_wrap').fadeOut()
                        houtai.hint('<p class="text-success">修改成功了</p>')
                        houtai.shop_list_ajax(houtai.list_ajax_page)
                    }else{
                        houtai.hint(res.message)
                    }
                }

            });
        },
        remove:function(){
            $.ajax({
                url: ip + 'index.php/admin/stuff/del',
                type: "post",
                data: {
                    stuffId:houtai.linshi_id,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res)
                    console.log(res)
                    if(res.resStatus===0){
                        houtai.hint('<p class="text-success">删除成功了</p>')
                        houtai.shop_list_ajax(houtai.list_ajax_page)
                        $('#modal_wrap').fadeOut()
                    }else{
                        houtai.hint(res.message)
                    }
                }

            });
        },
        // 把获取的商品数组写入table
        table_string: function (res) {
            console.log(res);
            // 写入table
            var string_head = "<thead> <tr> <th>编号</th> <th>产品名称</th>  <th>型号</th> <th>数量</th> <th>类型</th>  <th>单位</th> <th>入库历史</th> <th>操作</th> </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var ruku = !o.putStorage.amount  ? "无" :'<div class="item_history"> <div title="点击查看所有历史"> <b><span>总数量:</span><b class="inbound_tatle">' + o.putStorage.amount + '</b></b><span title="查看全部历史" class="btn btn-info btn-xs inbound_all">全部</span></div> <p><span>数量：</span>' + o.putStorage.amount + '</p><p><span>备注：</span>' + o.putStorage.remark + '</p><p>' + o.putStorage.time + '</p>  </div> ' ;
                    string_body += '<tr>' +
                        '<td class="item_id">' + o.stuffId + '</td>' +
                        '<td class="item_name">' + o.name + '</td>' +
                        '<td class="item_spec">' + o.spec + '</td>' +
                        '<td class="item_amount">' + o.amount + '</td>' +
                        '<td class="item_class">'+(o.types===0?"机械":"材料")+'</td>' +
                        '<td class="item_unit">' + o.unit + '</td>' +
                        '<td style="width: 25%;vertical-align: top;" class="inbound_history">' + ruku + '</td>' +
                        '<td style="width:200px;"> <b class="btn btn-warning btn-xs set">修改</b>  <b class="btn btn-danger btn-xs remove">删除</b>  <b class="btn btn-info btn-xs inbound">入库</b></td> </tr>';
                });
            } else{
                string_body = "<tr class='text-center'><td colspan='8'>没有数据</td></tr>";
            }

            $("#repertory_table").empty().append(string_head + string_body);
        },
        //初始类
        chushi_class: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/product",
                type: "post",
                data: {
                    token:get_cookie("key")
                },
                async: false,
                success: function (_res) {
                    var res=JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.arr = res.data;
                        for (var ii = 0, string11 ='<option value="">全部</option>'; ii < houtai.arr.length; ii++) {
                            string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
                        }
                        $("#select1").empty().append(string11);
                        $("#select2").empty().append('<option value="">全部</option>');
                        $("#select3").empty().append('<option value="">全部</option>');
                    }
                },
                error:function () {
                    houtai.hint("初始类失败！！");
                }
            });
        },
        //商品 入库 出库
        repertory_ajax: function () {
            //入库
            if (houtai.repertory_type == 1) {
                $.ajax({
                    url: ip + "index.php/admin/stuffStorage/add",
                    type: "post",
                    data: {
                        types:0,
                        stuffId:houtai.linshi_id,
                        receiveTime:new Date($('#inbound_time').val()).getTime()/1000,
                        supplierId:$.trim($('#inbound_supplier').val()),
                        amount:$.trim($('#inbound_n_n').val()),
                        price:$.trim($('#inbound_price').val()),
                        projectId:$('#inbound_gongdi_name').val(),
                        payWay:$('#inbound_pty_type option:selected').text(),
                        remark:$.trim($('#inbound_mark').val()),
                        receipt:$.trim($('#inbound_danju').val()),
                        agent:$.trim($('#inbound_agent').val()),
                        token: get_cookie("key")
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        console.log(res);
                        if (res.resStatus === 0) {
                            $("#repertory_inbound").fadeOut();
                            $(".input_is").val("");
                            $("#inbound_mark").val("");
                            houtai.hint("<p class='text-success'>入库成功了！</p>");
                            houtai.shop_list_ajax(houtai.list_ajax_page);
                        } else if (res.resStatus === 11) {
                            alert("您的账户被他人登录，请重新登录！！！");
                            location.href = "index.html";
                        }else{
                            houtai.hint(res.message);
                        }

                    }
                });
            }
        },
        set_record:function(){
            $.ajax({
                url: ip + "index.php/admin/stuffStorage/edit",
                type: "post",
                data: {
                    types:0,
                    storageId:houtai.record_id,
                    receiveTime:new Date($('#set_inbound_time').val()).getTime()/1000,
                    supplierId:$.trim($('#set_inbound_supplier').val()),
                    amount:$.trim($('#set_inbound_n_n').val()),
                    price:$.trim($('#set_inbound_price').val()),
                    projectId:$('#set_inbound_gongdi_name').val(),
                    payWay:$('#set_inbound_pty_type option:selected').text(),
                    remark:$.trim($('#set_inbound_mark').val()),
                    receipt:$.trim($('#set_inbound_danju').val()),
                    agent:$.trim($('#set_inbound_agent').val()),
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    if (res.resStatus === 0) {
                        $("#set_inbound").fadeOut();
                        $("#set_inbound .input_is").val("");
                        $("#set_inbound_mark").val("");
                        houtai.hint("<p style='color:#0f0'>记录修改成功！</p>");
                        houtai.record_all_ajax(houtai.linshi_record_page);
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                    } else if (res.resStatus === 11) {
                        alert("您的账户被他人登录，请重新登录！！！");
                        location.href = "index.html";
                    }else{
                        houtai.hint(res.message);
                    }

                }
            });
        },
        remove_record:function(){
            $.ajax({
                url: ip + "index.php/admin/stuffStorage/delete",
                type: "post",
                data: {
                    token: get_cookie("key"),
                    storageId:houtai.record_id
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    if(res.resStatus===0){
                        houtai.record_all_ajax(houtai.linshi_record_page);
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $('.modal').fadeOut()
                        houtai.hint('<p style="color:#0f0">删除成功了</p>')
                    }

                }
            });
        },
        // 获取全部记录的数组
        record_all_ajax: function (page_number) {
            var n = page_number===undefined ?1 :page_number ;
            var ispring=n!==0;
            $.ajax({
                url: ip + 'index.php/admin/stuffStorage/getStorageList',
                type: "post",
                async:ispring,
                data: {
                    stuffId:houtai.linshi_id,
                    types:houtai.linshi_record.type,
                    search:houtai.linshi_record.search,
                    payWay:houtai.linshi_record.pay_type ,
                    supplier:houtai.linshi_record.supplier,
                    startTime:houtai.linshi_record.start_time,
                    endTime:houtai.linshi_record.end_time,
                    page:n,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    houtai.record_all_string(res);
                    $("#page1").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.linshi_record_page=pageIndex;
                            houtai.record_all_ajax(pageIndex);
                        }
                    });
                }

            });
        },
        // 把获取的记录数组写入table
        record_all_string: function (res) {
            console.log(res);
            var jiaojieren= houtai.linshi_record.type==1?"收货人":"供应商";
            // 写入table
            var string_head = "<thead> <tr><th>ID</th><th>日期</th><th>供应商</th><th>名称</th><th>型号</th><th>单位</th><th>数量</th><th>价格</th><th>总价</th><th>单据号</th> <th>工地名称</th><th>付款方式</th><th>操作人</th><th>备注</th><th>操作</th></tr> </thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    console.log(o)
                    string_body += '<tr>' +
                        '<td class="id" style="mso-number-format:\'\\@\';">' + o.storageId + '</td>'+
                        '<td class="time" style="mso-number-format:\'\\@\';">' + o.receiveTime + '</td>'+
                        '<td class="supplier" style="width:100px">' + o.supplier + '</td>'+
                        '<td>' + o.name + '</td>' +
                        '<td>' + o.spec + '</td>' +
                        '<td>' +  o.unit + '</td>'+
                        '<td class="amount" style="mso-number-format:\'\\@\';">' + o.amount + '</td>'+
                        '<td class="price" style="mso-number-format:\'\\@\';">' + o.price + '</td>'+
                        '<td class="totalPrice" style="mso-number-format:\'\\@\';">' + o.totalPrice + '</td>'+
                        '<td class="receipt">' + o.receipt + '</td>'+
                        '<td class="projectName" data-id="'+(o.project&&o.project.projectId)+'">' + (o.project&&o.project.projectName) + '</td>'+
                        '<td class="payWay">' + o.payWay + '</td>'+
                        '<td class="agent">' + o.agent + '</td>'+
                        '<td class="remark" style="max-width:150px">' + o.remark + '</td>'+
                        '<td style="width:100px"><button class="btn btn-danger btn-xs set">修改</button> <button class="btn btn-xs btn-warning remove">删除</button></td></tr>';
                });
            } else{
                string_body = "<tr class='text-center'><td colspan='6'>没有数据</td></tr>";
            }

            $("#add_taocan_table").empty().append(string_head + string_body);
        },
        //供应商全部数据请求
        gongyingshang_ajax:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/supplier/show",
                type: "post",
                data:{
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res);
                    if(res.resStatus===0){
                        var set_string='<option value="">请选择</option>';
                        var seller_string='<option value="">全部</option>';
                        $.each(res.data,function (i,o) {
                            set_string+='<option value="'+o.id+'">'+o.sitenames+'</option>';
                            seller_string+='<option value="'+o.id+'">'+o.sitenames+'</option>';

                        });
                        $("#inbound_supplier").empty().append(set_string);
                        $("#set_inbound_supplier").empty().append(seller_string);
                        $("#gongyingshang").empty().append(seller_string);
                    }else{
                        houtai.hint("供应商请求失败！！");
                    }
                },
                error:function () {
                    houtai.hint("供应商请求失败！！");
                }

            });
        },
        chushiSelect:function(){
            $.ajax({
                url: ip + "index.php?s=admincms/project/showlist",
                type: "post",
                data: {
                    token: get_cookie("key"),
                    search:houtai.linshi_search.search
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    if(res.resStatus===0){
                        if (res.data.length > 0) {
                            var string_brand="";
                            $.each(res.data, function (i, o) {
                                string_brand += '<option value="'+o.id+'">'+o.title+'</option>';
                            });
                            $("#inbound_gongdi_name").empty().append(string_brand);
                            $("#set_inbound_gongdi_name").empty().append(string_brand);
                        }
                    }

                }
            });
            $.ajax({
                url: ip + "index.php/admin/stuffStorage/getAgent",
                type: "post",
                data:{
                    token:get_cookie("key")
                },
                success: function (_res) {
                    //写入操作分类table
                    var res=JSON.parse(_res);
                    console.log(res);
                    if(res.resStatus===0){
                        if (res.data.length > 0) {
                            var string_brand="";
                            $.each(res.data, function (i, o) {
                                string_brand += '<li><a >' + o.agent + '</a><p style="display:none;">' + o.bid + '</p></li>';
                            });
                            $("#down_menu2").empty().append(string_brand);
                            $("#set_down_menu2").empty().append(string_brand);
                        }
                    }


                },
                error:function () {
                    houtai.hint("失败！！");
                }

            });
        }
    };
    houtai.shop_list_ajax();
    houtai.gongyingshang_ajax();
    houtai.chushiSelect()
// 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });

    //搜索库存商品
    $("#search_btn").bind("click", function () {
        if ($.trim($("#search").val()) === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.search = $.trim($("#search").val());
            houtai.shop_list_ajax();
        }
    });
    //回车按钮操作
    $("#search_kucun_form").submit(function () {
        if ($.trim($("#search").val()) === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.search = $.trim($("#search").val());
            houtai.shop_list_ajax();
        }
        return false;
    });

    //添加显示按钮
    $("#add_item").bind('click',function () {
       $('#add_item_wrap').fadeIn();
    });
    //修改事件
    $("#add_item_confirm").bind("click", function () {
        if (houtai.jiancha()) {
            houtai.add();
        }
    });
    //修改事件
    $("#set_item_confirm").bind("click", function () {
        if (houtai.jiancha()) {
            houtai.set();
        }
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 修改取消
    $(".cancle_set").bind("click", function () {
        $(this).parents(".set_shangpin").fadeOut(200);

    });
    //历史查询弹框关闭
    $("#close_table_history").bind("click", function () {
        $("#repertory_table_history").fadeOut(200);
    });
    //查看全部入库历史
    $("#repertory_table").on("click", ".inbound_history .inbound_all", function () {
        houtai.repertory_type = 0;
        houtai.linshi_id = $(this).parents("tr").find(".item_id").text();
        houtai.record_all_ajax()
        $('#repertory_record').fadeIn()
    });
    //查看全部出库历史
    $("#repertory_table").on("click", ".outbound_history .outbound_all", function () {
        houtai.repertory_type = 1;
        houtai.linshi_id = $(this).parents("tr").find(".item_id").text();
        houtai.record_all_ajax()
        $('#repertory_record').fadeIn()
    });


    $('#down_menu1').on('click','li',function () {
       $('#inbound_gongdi_name').val($(this).find('a').text())
    });
    $('#down_menu2').on('click','li',function () {
        $('#inbound_agent').val($(this).find('a').text());
    });

    $('#set_down_menu1').on('click','li',function () {
       $('#set_inbound_gongdi_name').val($(this).find('a').text())
    });
    $('#set_down_menu2').on('click','li',function () {
        $('#set_inbound_agent').val($(this).find('a').text())
    });


    //修改按钮
    $("#repertory_table").on("click", ".set", function (){
        houtai.linshi_id=$(this).parents('tr').find('.item_id').text();
        $('#set_item_wrap').fadeIn();
        $('#set_item_name').val($(this).parents('tr').find('.item_name').text());
        $('#set_item_unit').val($(this).parents('tr').find('.item_unit').text());
        $('#set_item_spec').val($(this).parents('tr').find('.item_spec').text());
        console.log(houtai.select_in_text($(this).parents('tr').find('.item_class').text(),$('#set_item_class')))
        $('#set_item_class').val(houtai.select_in_text($(this).parents('tr').find('.item_class').text(),$('#set_item_class')));
    })
    //删除按钮
    $("#repertory_table").on("click", ".remove", function (){
        houtai.type=1
        houtai.linshi_id=$(this).parents('tr').find('.item_id').text();
         $('.modal-body').empty().append('<p>你确定要删除 <span style="color:#f00;">'+$(this).parents('tr').find('.item_name').text()+' </span>吗？<p>')
         $('#modal_wrap').show();
    })
    $('#confirm').bind('click',function () {
       if(houtai.type===1){
           houtai.remove()
       }
       if(houtai.type===2){
           houtai.remove_record()
       }

    });






    //入库按钮
    $("#repertory_table").on("click", ".inbound", function () {
        houtai.linshi_id = $(this).parents("tr").find(".item_id").text();
        houtai.repertory_type = 1;
        $(".set_shangpin").hide();
        $("#repertory_inbound").fadeIn();
        $("#inbound_name").val($(this).parents("tr").find(".item_name").text());
        $("#inbound_number").val($(this).parents("tr").find(".item_id").text());
        $("#inbound_y_n").val($(this).parents("tr").find(".item_amount").text());
    });

    // //出库按钮
    // $("#repertory_table").on("click", ".outbound", function () {
    //     houtai.linshi_id = $(this).parents("tr").find(".item_id").text();
    //     houtai.repertory_type = 2;
    //     $(".set_shangpin").hide();
    //     $("#repertory_outbound").fadeIn();
    //     $("#outbound_name").val($(this).parents("tr").find(".item_name").text());
    //     $("#outbound_number").val($(this).parents("tr").find(".item_id").text());
    //     $("#outbound_y_n").val($(this).parents("tr").find(".item_amount").text());
    // });
    $('#inbound_price').bind('input',function () {
        console.log($('#inbound_price').val()*$('#inbound_n_n').val())
        $('#inbound_total_price').val(Math.float_($('#inbound_price').val()*$('#inbound_n_n').val(),2));
    });
    $('#inbound_n_n').bind('input',function () {
        console.log($('#inbound_price').val()*$('#inbound_n_n').val())
        $('#inbound_total_price').val(Math.float_($('#inbound_price').val()*$('#inbound_n_n').val(),2));
    });
    $('#set_inbound_price').bind('input',function () {
        console.log($('#set_inbound_price').val()*$('#set_inbound_n_n').val())
        $('#set_inbound_total_price').val(Math.float_($('#set_inbound_price').val()*$('#set_inbound_n_n').val(),2));
    });
    $('#set_inbound_n_n').bind('input',function () {
        console.log($('#set_inbound_price').val()*$('#set_inbound_n_n').val())
        $('#set_inbound_total_price').val(Math.float_($('#set_inbound_price').val()*$('#set_inbound_n_n').val(),2));
    });
    $("#repertory_table").on("click", ".toggle_warnAmount", function () {
        if(houtai.linshi_search.warnAmount===null){
            houtai.linshi_search.warnAmount=false;
        }else if(houtai.linshi_search.warnAmount===true){
            houtai.linshi_search.warnAmount=false;
        }else{
            houtai.linshi_search.warnAmount=true;
        }
        houtai.shop_list_ajax();

    });
    //入库确认按钮
    $(".add").bind("click", function () {
        if (houtai.click_yes()) {
            if (houtai.jiancha()) {
                houtai.repertory_ajax();
            }
        }

    });
    //修改确认按钮
    $(".set").bind("click", function () {
        if (houtai.click_yes()) {
            if (houtai.jiancha()) {
                houtai.set_record();
            }
        }

    });
    //导出excel
    $("#print_repertory").bind("click",function () {

        if($("#repertory_table tr:last>td").length>1){
            houtai.shop_list_ajax(0);
            var title="库存管理-"+$("#select1 option:selected").text()+"-"+$("#select2 option:selected").text();
            houtai.excel($("#repertory_table")[0],title);
        }else{
            houtai.hint("没有什么导出的！！");
        }

    });
    //记录查询按钮
    $("#record_btn").bind("click",function () {
        $("#repertory_record").fadeIn();
        houtai.linshi_id='';
        houtai.record_all_ajax();
    });
    //记录修改
    $('#add_taocan_table').on('click','.set',function () {
        houtai.record_id=$(this).parents('tr').find('.id').text();
        $('#set_inbound_n_n').val($(this).parents('tr').find('.amount').text());
        $('#set_inbound_price').val($(this).parents('tr').find('.price').text());
        $('#set_inbound_total_price').val($(this).parents('tr').find('.totalPrice').text());
        $('#set_inbound_gongdi_name').val($(this).parents('tr').find('.projectName').attr('data-id'));
        $('#set_inbound_pty_type').val(houtai.select_in_text($(this).parents('tr').find('.payWay').text(),$('#set_inbound_pty_type')));
        $('#set_inbound_danju').val($(this).parents('tr').find('.receipt').text());
        $('#set_inbound_time').val($(this).parents('tr').find('.time').text());
        $('#set_inbound_supplier').val(houtai.select_in_text($(this).parents('tr').find('.supplier').text(),$('#set_inbound_supplier')));
        $('#set_inbound_agent').val($(this).parents('tr').find('.agent').text());
        $('#set_inbound_mark').val($(this).parents('tr').find('.remark').text());
       $('#set_inbound').fadeIn();
    });
    //记录删除
    $('#add_taocan_table').on('click','.remove',function () {
        houtai.record_id=$(this).parents('tr').find('.id').text();
        houtai.type=2;
        $('.modal-body').empty().append('您确定要删除这条记录吗?');
        $('#modal_wrap').fadeIn();
    });


    //记录类型选择下拉
    $("#bound_type").bind("change",function () {
        houtai.linshi_record.search=[];
        houtai.linshi_record.supplier='';
        houtai.linshi_record.pay_type='';
        houtai.linshi_record.type=$(this).val();
        houtai.linshi_record.start_time='';
        houtai.linshi_record.end_time='';
        houtai.record_all_ajax();
    });
    //支付方式选择下拉
    $("#pay_type").bind("change",function () {
        houtai.linshi_record.search=[];
        houtai.linshi_record.supplier='';
        houtai.linshi_record.pay_type=$(this).val()===''?'':$(this).find('option:selected').text();
        houtai.linshi_record.type='';
        houtai.linshi_record.start_time='';
        houtai.linshi_record.end_time='';
        houtai.record_all_ajax();
    });
    //供应商选择下拉
    $("#gongyingshang").bind("change",function () {
        houtai.linshi_record.search=[];
        houtai.linshi_record.supplier=$(this).val();
        houtai.linshi_record.pay_type='';
        houtai.linshi_record.type='';
        houtai.linshi_record.start_time='';
        houtai.linshi_record.end_time='';
        houtai.record_all_ajax();
    });
   //搜索记录
    $('#search_jilu_btn').bind('click',function () {
        if($('#search_jilu').val()===''){
            houtai.hint("请输入后再查询");
            $("#search_jilu").focus();
        }else{
            houtai.linshi_record.search=$.trim($('#search_jilu').val()).split(/,|，/);
            houtai.linshi_record.supplier='';
            houtai.linshi_record.pay_type='';
            houtai.linshi_record.type='';
            houtai.linshi_record.start_time='';
            houtai.linshi_record.end_time='';
            houtai.record_all_ajax();
        }
    });
    // 按时间搜索
    $("#search_time").bind("click", function () {
        if ($.trim($("#input_time").val()) === "") {
            $("#input_time").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                // 匹配的正则表达式
                var regexp = /(\d+-\d+-[^-]+)-(.+)/;
                houtai.linshi_record.search=[];
                houtai.linshi_record.supplier='';
                houtai.linshi_record.pay_type='';
                houtai.linshi_record.type='';
                houtai.linshi_record.start_time = new Date($("#input_time").val().match(regexp)[1]).getTime() / 1000;
                houtai.linshi_record.end_time= new Date($("#input_time").val().match(regexp)[2]).getTime() / 1000;
                houtai.record_all_ajax();
            }
        }

    });

    //时间插件
    $('#input_time').daterangepicker({
        timePicker:true,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    //时间插件
    $('#inbound_time').daterangepicker({
        timePicker:true,
        singleDatePicker:true,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });    //时间插件
    $('#set_inbound_time').daterangepicker({
        singleDatePicker:true,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    $("#input_time_btn").bind("click", function () {
        $("#input_time").click();
    });



    //取消记录框按钮
    $("#close_record").bind("click", function () {
        $("#repertory_record").fadeOut();
    });
    //所有记录打印按钮
    $("#record_all_btn").bind("click",function () {
        if($("#add_taocan_table tr:last>td").length>1){
            houtai.record_all_ajax(0);
            houtai.excel($("#add_taocan_table")[0],"库存记录");
        }else{
            houtai.hint("没有什么导出的！！");
        }
    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});